<?php

use BarcodeBakery\Common\BCGColor;
use BarcodeBakery\Common\BCGDrawing;
use BarcodeBakery\Common\BCGFontFile;
use BarcodeBakery\Barcode\BCGcode39;

class BarcodeHelper {

    /**
     * code39
     * option 参数
     *  - scale: 条形码尺寸
     *  - font: 显示内容大小
     *  - thickness: 条形码上下厚度
     *  - rotation: 条形码旋转角度，通常0为横向，90度为纵向
     * @param type $text
     * @param type $savePath
     * @param type $option
     * @return type
     * @throws Exception
     */
    public static function createCode39($text, $savePath, $option = []){

        require_once dirname(__FILE__).'/Barcode/autoload.php';
        $option = array_merge(array(
            'scale' => 1,
            'font' => 18,
            'thickness' => 40,
            'rotation' => 90), $option);

        // Loading Font
        $font = new BCGFontFile(dirname(__FILE__).'/Barcode/font/Arial.ttf', $option['font']);

        // The arguments are R, G, B for color.
        $colorBlack = new BCGColor(0, 0, 0);
        $colorWhite = new BCGColor(255, 255, 255);

        try {
            $code = new BCGcode39();
            $code->setScale($option['scale']); // Resolution
            $code->setThickness($option['thickness']); // Thickness
            $code->setForegroundColor($colorBlack); // Color of bars
            $code->setBackgroundColor($colorWhite); // Color of spaces
            $code->setFont($font); // Font (or 0)
            $code->setChecksum(false);
            $code->parse($text); // Text
        } catch (Exception $exception) {
            throw new Exception('failed: '.$exception->getMessage());
        }

        /* Here is the list of the arguments
        1 - Filename (empty : display on screen)
        2 - Background color */
        $drawing = new BCGDrawing($savePath, $colorWhite);
        $drawing->setBarcode($code);
        $drawing->setRotationAngle($option['rotation']);//增加
        $drawing->draw();
        $drawing->finish(BCGDrawing::IMG_FORMAT_PNG);
        return FS::isFile($savePath) ? true : false;
    }
}